package openadmin.util.reflection;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import openadmin.dao.Base;

/**
  * To get the type, the name and the value of the fields of the class 
  * @version  0.1
  * Created 10-05-2009  
  * Author Alfred Oliver
  *
  */
public class ReflectionField {

	/** String and the type, name and value of the field */
	private String propertyField[];
	
	/** List of fields with their properties (type, name and value) */
	private List<String[]> fields;
	
	
	/**
	 * Get all the fields initialized
	 * @param obj  object type Base
	 * @return List of fields with their properties (type, name and value)
	 * */
	public List<String[]> execute(Base obj) {
						
		fields = new ArrayList<String[]>(0);				
				
		try {
		
		for (Field f: obj.getClass().getDeclaredFields()){
			
			propertyField = new String[3];
									
			//View of fields 
			f.setAccessible(true);															
			
			if (f.get(obj) == null || 
				f.getName().equals("debuglog") ||
				f.getName().equals("historiclog") ||
				f.getType().getSimpleName().equals("Set")) continue;
																											
			//Field of type object (openadmin.dao)			
			if (f.getType().getName().startsWith("openadmin.dao")){
				
				if (((Base)f.get(obj)).getDescription() == null) continue;
				
				propertyField[0] = obj.getClass().getSimpleName() + "." + f.getType().getSimpleName() + ".description";
				propertyField[1] = "=";		
				propertyField[2] = ((Base)f.get(obj)).getDescription().trim();												
												
				fields.add(propertyField);	
				
				continue;
			}
									
			propertyField[0] = obj.getClass().getSimpleName() + "." + f.getName();
			propertyField[1] = this.operator(f.getType().getSimpleName());
			propertyField[2] = f.get(obj).toString().trim();
			
			fields.add(propertyField);			
												
		}				
		
		} catch (IllegalArgumentException e) {
			
			e.printStackTrace();
		
		} catch (IllegalAccessException e) {
			
			e.printStackTrace();
		}
		return fields;
	}
	
	
	/**
	 * Get all the fields initialized
	 * @param String  Identifies the declared type for the field
	 * @return Type logical operator
	 * */
	private String operator (String pType) {
		
		if (pType.equals("String")) return "like";
		
		else if (pType.equals("boolean")) return "=";
		
		else if (pType.equals("Date")) return "=";
		
		return null;
		
	}
}
